package com.ruoyi.zq.controller;

import com.alibaba.excel.EasyExcel;
import com.ruoyi.common.zq.domin.ZqVolunteerActivity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 测试easyExcel
 *
 * @Author: LoneyFeather
 * @Date: Created in 14:23 2022/2/14
 */

@RestController
@RequestMapping("/easy")
public class TestEasyExcel {

    @GetMapping("download")
    public void download(HttpServletResponse response, @RequestBody ZqVolunteerActivity zqVolunteerActivity) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        System.out.println("请求的参数");
        System.out.println(zqVolunteerActivity.toString());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), TestExcelModel.class).sheet("模板").doWrite(data());
    }

    private List<TestExcelModel> data() {
        List<TestExcelModel> data = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            TestExcelModel model = new TestExcelModel();
            model.setId(i+1);
            model.setName("名字00"+(i+1));
            model.setPass("密码："+i);
            model.setAge(i+1);
            data.add(model);
        }
        return data;
    }
}
